<?php
/**
 * Class that operate on table 'jobs'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class JobsMySqlDAO implements JobsDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return JobsMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM jobs WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM jobs';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM jobs ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param job primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM jobs WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param JobsMySql job
 	 */
	public function insert($job){
		$sql = 'INSERT INTO jobs (company_name, description, logo, type, pay, job_location, telephone, visa_type, address, email, start_date, end_date, web, created_by, create_date, updated_by, updated_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($job->companyName);
		$sqlQuery->set($job->description);
		$sqlQuery->set($job->logo);
		$sqlQuery->set($job->type);
		$sqlQuery->set($job->pay);
		$sqlQuery->set($job->jobLocation);
		$sqlQuery->set($job->telephone);
		$sqlQuery->set($job->visaType);
		$sqlQuery->set($job->address);
		$sqlQuery->set($job->email);
		$sqlQuery->set($job->startDate);
		$sqlQuery->set($job->endDate);
		$sqlQuery->set($job->web);
		$sqlQuery->setNumber($job->createdBy);
		$sqlQuery->set($job->createDate);
		$sqlQuery->setNumber($job->updatedBy);
		$sqlQuery->set($job->updated_date);
		$sqlQuery->setNumber($job->status);

		$id = $this->executeInsert($sqlQuery);	
		$job->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param JobsMySql job
 	 */
	public function update($job){
		$sql = 'UPDATE jobs SET company_name = ?, description = ?, logo = ?, type = ?, pay = ?, job_location = ?, telephone = ?, visa_type = ?, address = ?, email = ?, start_date = ?, end_date = ?, web = ?, created_by = ?, create_date = ?, updated_by = ?, updated_date = ?, status = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($job->companyName);
		$sqlQuery->set($job->description);
		$sqlQuery->set($job->logo);
		$sqlQuery->set($job->type);
		$sqlQuery->set($job->pay);
		$sqlQuery->set($job->jobLocation);
		$sqlQuery->set($job->telephone);
		$sqlQuery->set($job->visaType);
		$sqlQuery->set($job->address);
		$sqlQuery->set($job->email);
		$sqlQuery->set($job->startDate);
		$sqlQuery->set($job->endDate);
		$sqlQuery->set($job->web);
		$sqlQuery->setNumber($job->createdBy);
		$sqlQuery->set($job->createDate);
		$sqlQuery->setNumber($job->updatedBy);
		$sqlQuery->set($job->updated_date);
		$sqlQuery->setNumber($job->status);

		$sqlQuery->setNumber($job->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM jobs';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCompanyName($value){
		$sql = 'SELECT * FROM jobs WHERE company_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM jobs WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLogo($value){
		$sql = 'SELECT * FROM jobs WHERE logo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByType($value){
		$sql = 'SELECT * FROM jobs WHERE type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPay($value){
		$sql = 'SELECT * FROM jobs WHERE pay = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByJobLocation($value){
		$sql = 'SELECT * FROM jobs WHERE job_location = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTelephone($value){
		$sql = 'SELECT * FROM jobs WHERE telephone = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisaType($value){
		$sql = 'SELECT * FROM jobs WHERE visa_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAddress($value){
		$sql = 'SELECT * FROM jobs WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM jobs WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStartDate($value){
		$sql = 'SELECT * FROM jobs WHERE start_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEndDate($value){
		$sql = 'SELECT * FROM jobs WHERE end_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByWeb($value){
		$sql = 'SELECT * FROM jobs WHERE web = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedBy($value){
		$sql = 'SELECT * FROM jobs WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreateDate($value){
		$sql = 'SELECT * FROM jobs WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedBy($value){
		$sql = 'SELECT * FROM jobs WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM jobs WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM jobs WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByCompanyName($value){
		$sql = 'DELETE FROM jobs WHERE company_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM jobs WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLogo($value){
		$sql = 'DELETE FROM jobs WHERE logo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByType($value){
		$sql = 'DELETE FROM jobs WHERE type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPay($value){
		$sql = 'DELETE FROM jobs WHERE pay = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByJobLocation($value){
		$sql = 'DELETE FROM jobs WHERE job_location = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTelephone($value){
		$sql = 'DELETE FROM jobs WHERE telephone = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisaType($value){
		$sql = 'DELETE FROM jobs WHERE visa_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAddress($value){
		$sql = 'DELETE FROM jobs WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM jobs WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStartDate($value){
		$sql = 'DELETE FROM jobs WHERE start_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEndDate($value){
		$sql = 'DELETE FROM jobs WHERE end_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByWeb($value){
		$sql = 'DELETE FROM jobs WHERE web = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedBy($value){
		$sql = 'DELETE FROM jobs WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreateDate($value){
		$sql = 'DELETE FROM jobs WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedBy($value){
		$sql = 'DELETE FROM jobs WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM jobs WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM jobs WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return JobsMySql 
	 */
	protected function readRow($row){
		$job = new Job();
		
		$job->id = $row['id'];
		$job->companyName = $row['company_name'];
		$job->description = $row['description'];
		$job->logo = $row['logo'];
		$job->type = $row['type'];
		$job->pay = $row['pay'];
		$job->jobLocation = $row['job_location'];
		$job->telephone = $row['telephone'];
		$job->visaType = $row['visa_type'];
		$job->address = $row['address'];
		$job->email = $row['email'];
		$job->startDate = $row['start_date'];
		$job->endDate = $row['end_date'];
		$job->web = $row['web'];
		$job->createdBy = $row['created_by'];
		$job->createDate = $row['create_date'];
		$job->updatedBy = $row['updated_by'];
		$job->updated_date = $row['updated_date'];
		$job->status = $row['status'];

		return $job;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return JobsMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>